local blaze = require "blaze"
local SimpleListItem = require "blaze.ui.widget.SimpleListItem"

local M = blaze.class(SimpleListItem)

local function update_selected_state(self, selected)
    if selected then
        self.selected:SetActive(true)
        self.unselected:SetActive(false)
    else
        self.selected:SetActive(false)
        self.unselected:SetActive(true)
    end
end

function M:ctor(route, index)
    self.route = route
    self.res = route.meta.res
    self.index = index
end

function M:on_injected(bind)
    local current = self.app.router.current()
    local selected = current.meta == self.route.meta
    update_selected_state(self, selected)
    bind("button.click", self.button, self.on_click)
end

function M:on_click()
    local current = self.app.router.current()
    local parent = current.matched[1] -- TODO:不一定是第一级路由
    self.app.router.replace(parent.path .. "/" .. self.route.path)
end

function M:before_route_update(to, from)
    local selected = to.meta == self.route.meta
    update_selected_state(self, selected)
end

return M